package com.atguigu.gmall.web.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.feign.cart.CartFeignClient;
import com.atguigu.gmall.product.entity.SkuInfo;
import feign.Request;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author lfy
 * @Description
 * @create 2022-12-16 10:40
 */
@Controller
public class CartController {


    @Autowired
    CartFeignClient cartFeignClient;

    /**
     * 把商品添加到购物车
     *
     * @param skuId
     * @param num
     * @return
     */
    @GetMapping("/addCart.html")
    public String addToCart(@RequestParam("skuId") Long skuId,
                            @RequestParam("skuNum") Integer num,
                            Model model) {
        //远程调用购物车  OOM；
        //List、Map、Set容器类。一定要想清楚，容器里面的数据什么时候放，一定要有删。

//        map.put(Thread.currentThread(),request);
//        threadLocal.set(request);
        //controller的所有逻辑调用是否是同一个线程
        Request.Options options = new Request.Options(5000,5000);
        Result<SkuInfo> result = cartFeignClient.addToCart(skuId, num,options);

        //远程结束了，拦截器运行完了
//        threadLocal.remove();
//        map.remove(Thread.currentThread()); //防止OOM
        //{skuDefaultImg、skuName、id}
        model.addAttribute("skuInfo", result.getData());

        model.addAttribute("skuNum", num);
        return "cart/addCart";//添加成功提示页
    }

    @GetMapping("/cart.html")
    public String cartListPage(){

        return "cart/index";
    }

    @GetMapping("/cart/deleteChecked")
    public String deleteChecked(){

        cartFeignClient.deleteChecked();

        return "redirect:http://cart.gmall.com/cart.html";
    }
}
